package service

import (
	"ruanGongKeShe16Zu/db"
	"ruanGongKeShe16Zu/model"
	"ruanGongKeShe16Zu/repository"
	"ruanGongKeShe16Zu/service/vo"
	"sync"

	"github.com/gin-gonic/gin"
)

// 单例实现
var CommentServiceInstance *CommentService
var CommentServiceOnce sync.Once

type CommentService struct {
	Repository repository.CommentRepository
}

func GetCommentService() *CommentService {
	CommentServiceOnce.Do(func() {
		repository := repository.NewCommentRepository().(repository.CommentRepository)
		repository.DB.AutoMigrate(&model.Comment{})
		CommentServiceInstance = &CommentService{
			Repository: repository,
		}
	})
	return CommentServiceInstance
}

func (s *CommentService) Create(ctx *gin.Context, req *vo.CommentReq) (*model.Comment, error) {
	// instru, _ := repository.NewInstrumentRepository().SelectById(req.InstrumentID)
	// user, _ := ctx.Get("user")
	var user model.User
	db.GetDB().Where("id = ?", req.UserID).First(&user)
	commentModel := model.Comment{
		InstrumentID: req.InstrumentID,
		Content:      req.Content,
		UserID:       req.UserID,
	}
	comment, err := s.Repository.Create(&commentModel)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return nil, err
	}
	return comment, nil
}

func (s *CommentService) Retrieve(ctx *gin.Context, id int) (*model.Comment, error) {
	comment, err := s.Repository.SelectById(id)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return nil, err
	}
	return comment, nil
}

func (s *CommentService) Update(ctx *gin.Context, item *model.Comment, requestItem *vo.CommentReq) (*model.Comment, error) {
	comment, err := s.Repository.Update(*item, *requestItem)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return nil, err
	}
	return comment, nil
}

func (s *CommentService) Delete(ctx *gin.Context, id int) error {
	err := s.Repository.DeleteById(id)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return err
	}
	return nil
}

// CommentList 列举分类
func (s *CommentService) CommentList(ctx *gin.Context, req *vo.ListCommentReq) (resp *vo.DataListResp, err error) {
	comments, err := s.Repository.CommentList(req)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return
	}
	Resp := make([]*vo.ListCommentResp, 0)

	for _, v := range comments {
		var user model.User
		db.GetDB().Where("id = ?", v.UserID).First(&user)
		Resp = append(Resp, &vo.ListCommentResp{
			InstrumentID: v.InstrumentID,
			Content:      v.Content,
			UserID:       v.UserID,
			UserName:     user.Name,
			CreatedAt:    v.CreatedAt.Local().Format("2006-01-02 15:04:05"),
		})
	}

	resp = &vo.DataListResp{
		Item:  Resp,
		Total: int64(len(Resp)),
	}

	return
}

func (s *CommentService) SelectByUserID(ctx *gin.Context, req *vo.ListCommentReq, id uint) (resp *vo.DataListResp, err error) {
	comments, err := s.Repository.SelectByInstrumentID(req, id)
	if err != nil {
		// utils.LogrusObj.Error(err)
		return
	}
	Resp := make([]*vo.ListCommentResp, 0)
	for _, v := range comments {
		var user model.User
		db.GetDB().Where("id = ?", v.UserID).First(&user)
		Resp = append(Resp, &vo.ListCommentResp{
			InstrumentID: v.InstrumentID,
			Content:      v.Content,
			UserID:       v.UserID,
			UserName:     user.Name,
			CreatedAt:    v.CreatedAt.Local().Format("2006-01-02 15:04:05"),
		})
	}

	resp = &vo.DataListResp{
		Item:  Resp,
		Total: int64(len(Resp)),
	}

	return
}
